<template>
  <div class="son-page-container" :style="{ backgroundImage: `url(${website_bg_url})` }">
    <el-container class="main-container">
      <el-header height="10vh" class="header-container">
        <MyHeader />
      </el-header>
      <el-main>
        <el-row :gutter="20">
          <el-col :span="4">
            <div class="area-container menu-container">
              <div class="sub-menu-title">视频分类</div>
              <div class="menu-item" v-for="(item, index) in menuItems" :key="index">
                <div class="parent-menu" @click="toggleSubmenu(index)">
                  <span class="menu-icon">{{ item.icon }}</span>
                  <span class="menu-title">{{ item.title }}</span>
                  <el-icon class="expand-icon" :class="{ 'is-expanded': item.isOpen }">
                    <ArrowDown/>
                  </el-icon>
                </div>
                <div class="child-menu" v-show="item.isOpen">
                  <div
                    class="child-item"
                    v-for="(subItem, subIndex) in item.children"
                    :key="subIndex"
                    :class="{ selected: subItem.id === activeId }"
                    @click="handleMenuClick(subItem.id)"
                  >
                    {{ subItem.title }}
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="20">
            <div class="area-container right-area">
              <component :is="currentComponent" v-if="currentComponent" :key="componentKey" />
            </div>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import {onMounted, ref, computed, watch} from 'vue'
import axiosUtil from '../../axios-util/axios-util'
import { ArrowDown} from '@element-plus/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import ProductVideo from '../videoWarehouse/ProductVideo.vue'
import ProductPicture from '../videoWarehouse/ProductPicture.vue'
import SourceVideo from '../videoWarehouse/SourceVideo.vue'
import SourcePicture from '../videoWarehouse/SourcePicture.vue'
import SourceAudio from '../videoWarehouse/SourceAudio.vue'
import SourcePerson from '../videoWarehouse/SourcePerson.vue';
import MyHeader from '../common/MyHeader.vue';
import utils from '../../utils/utils'

//主题控制light  dark
const theme = utils.getTheme();
// 网站背景图
const website_bg_url = ref('')
const activeId = ref<number | null>(null)
const currentComponent = ref<any>(null)
const componentKey = ref(0)

const router = useRouter()
const route = useRoute()

const menuItems = ref([
  {
    id:1,
    title: '成品库',
    icon: '📁',
    isOpen: true,
    children: [
      { title: '视频库' ,id:11,pId:1},
      { title: '图片库',id:12,pId:1 },
      { title: 'PPT',id:13,pId:1 }
    ]
  },
  {
    id:2,
    title: '素材库',
    icon: '📁',
    isOpen: false,
    children: [
      { title: '视频库',id:21,pId:2 },
      { title: '图片库',id:22,pId:2 },
      { title: '音频库',id:23,pId:2 },
      { title: '数字人',id:24,pId:2 }
    ]
  }
])

const toggleSubmenu = (index: number) => {
  menuItems.value[index].isOpen = !menuItems.value[index].isOpen
}

const handleMenuClick = (id: number) => {
  activeId.value = id
  componentKey.value++ // 强制组件重新加载

  switch(id) {
    case 11:
      currentComponent.value = ProductVideo
      break
    case 12:
      currentComponent.value = ProductPicture
      break
    case 21:
      currentComponent.value = SourceVideo
      break
    case 22:
      currentComponent.value = SourcePicture
      break
    case 23:
      currentComponent.value = SourceAudio
      break
    case 24:
      currentComponent.value = SourcePerson
      break
    default:
      currentComponent.value = null
  }
}

const handleVideoLibrary = () => {
  router.push('/videoWarehouse')
}

onMounted(() => {
  document.body.classList.remove('login-page');
  initWebsiteBgUrl()

  // 从路由参数中获取id
  const menuId = route.query.id ? Number(route.query.id) : 11

  // 如果id存在且有效，则选中对应菜单
  if (menuId && menuItems.value.some(item =>
    item.children.some(child => child.id === menuId)
  )) {
    handleMenuClick(menuId)
  } else {
    // 如果id无效或不存在，默认选中第一个菜单项
    handleMenuClick(11)
  }
})

//读取配置，获取网站背景图
const initWebsiteBgUrl = async () => {
  const response = await axiosUtil.post('api/pc/site/get_agent_config', {})
  if (response.data.code === 0 && response.data.data) {
    website_bg_url.value = import.meta.env.VITE_APP_SERVER + response.data.data.website_bg || ''
  }
}
</script>

<style scoped>
.son-page-container {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.main-container {
  height: 100vh;
  width: 100%;
  backdrop-filter: blur(5px);
}

.header-container {
  padding: 0;
  width: 100%;
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.3)"');
  backdrop-filter: blur(10px);
  border-bottom: 1px solid v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(0, 0, 0, 0.4)"');
  box-shadow: 0 4px 6px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.1)" : "rgba(0, 0, 0, 0.3)"');
}

.el-main {
  padding: 20px;
  height: 90vh;
  overflow-x: hidden;
}

.el-row {
  height: 100%;
  margin: 0 !important;
}

.el-col {
  height: 100%;
}

.area-container {
  height: 100%;
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.1)" : "rgba(255, 255, 255, 0.3)"');
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
  box-shadow: 0 4px 6px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.1)" : "rgba(255, 255, 255, 0.3)"');
  transition: all 0.3s ease;
  padding: 15px;
}

/* .area-container:hover {
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.15)" : "rgba(255, 255, 255, 0.4)"');
  box-shadow: 0 6px 12px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.15)" : "rgba(255, 255, 255, 0.4)"');
  border-color: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.3)" : "rgba(255, 255, 255, 0.5)"');
} */

.menu-container {
  overflow-y: auto;
}

.menu-container::-webkit-scrollbar {
  width: 6px;
}

.menu-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}

.menu-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.sub-menu-title {
  color: v-bind('theme === "light" ? "rgba(0, 0, 0, 0.8)" : "#fff"');
  font-size: 16px;
  font-weight: 500;
  padding: 10px 0;
  margin-bottom: 10px;
  text-align: left;
  border-bottom: 1px solid v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
}

.menu-item {
  margin-bottom: 15px;
}

.parent-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.1)" : "rgba(255, 255, 255, 0.3)"');
  backdrop-filter: blur(10px);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
  color: #fff;
  font-weight: 500;
  box-shadow: 0 4px 6px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.1)" : "rgba(255, 255, 255, 0.3)"');
}

.parent-menu:hover {
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
  transform: translateY(-2px);
  box-shadow: 0 6px 12px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.15)" : "rgba(0, 0, 0, 0.4)"');
}

.menu-icon {
  margin-right: 10px;
  font-size: 1.1em;
  color: rgba(0, 255, 255, 0.8);
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.menu-title {
  flex: 1;
  color: v-bind('theme === "light" ? "rgba(0, 0, 0, 0.8)" : "#fff"');
  font-size: 0.95em;
  letter-spacing: 1px;
}

.expand-icon {
  transition: transform 0.3s ease;
  color: #fff;
}

.expand-icon.is-expanded {
  transform: rotate(180deg);
}

.child-menu {
  margin-top: 8px;
  padding: 5px;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.child-item {
  padding: 10px 15px;
  margin: 8px 0;
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.1)" : "rgba(255, 255, 255, 0.3)"');
  backdrop-filter: blur(10px);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
  color: v-bind('theme === "light" ? "rgba(0, 0, 0, 0.8)" : "#fff"');
  font-size: 0.95em;
  text-align: center;
  box-shadow: 0 2px 4px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.1)" : "rgba(255, 255, 255, 0.3)"');
}

.child-item:hover {
  background: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.2)" : "rgba(255, 255, 255, 0.4)"');
  transform: translateY(-2px);
  box-shadow: 0 4px 8px v-bind('theme === "light" ? "rgba(0, 0, 0, 0.15)" : "rgba(255, 255, 255, 0.3)"');
  border-color: v-bind('theme === "light" ? "rgba(255, 255, 255, 0.4)" : "rgba(255, 255, 255, 0.5)"');
}

.child-item:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.child-item.selected {
  background: rgba(64, 158, 255, 0.3);
  border-color: rgba(64, 158, 255, 0.5);
  color: v-bind('theme === "light" ? "rgba(0, 0, 0, 0.8)" : "#fff"');
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.2);
}

.right-area {
  display: flex;
  flex-direction: column;
}

/* 删除 spacer 相关样式 */
.spacer,
.spacer-content,
.video-library-btn,
.video-library-btn:hover,
.video-library-btn:active,
.btn-icon {
  display: none;
}
</style>

<style>
/* 隐藏全局背景图 */
body::before {
  display: none !important;
}

/* 优化页面背景图和滚动条 */
html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  overflow-y: hidden;
}
</style>
